﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * Original source PushButton Engine:
 * Copyright (C) 2009 PushButton Labs, LLC
 * For more information see http://www.pushbuttonengine.com
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using System;
using Microsoft.Xna.Framework;
using AAEngine.Engine;
using AAEngine.Engine.Debug;

namespace AAEngine.Rendering2D.SpriteSheet
{
    /// <summary>
    /// Divide a sprite sheet into fixed-size cells.
    /// </summary>
    public class FixedSizeDivider : ISpriteSheetDivider
    {
        private SpriteSheetComponent _owningSheet;

        /// <summary>
        /// The width of each frame.
        /// </summary>
        public int Width { get; set; }

        /// <summary>
        /// The height of each frame.
        /// </summary>
        public int Height { get; set; }

        /// <summary>
        /// The horizonal spacing between frames
        /// </summary>
        public int HorizontalSpacing { get; set; }

        /// <summary>
        /// The vertical spacing between frames
        /// </summary>
        public int VerticalSpacing { get; set; }

        public SpriteSheetComponent OwningSheet
        {
            set
            {
                if (_owningSheet != null)
                {
                    Logger.Warn(this, "OwningSheet", "Already assigned to a sheet, reassigning may result in unexpected behavior.");
                }
                _owningSheet = value;
            }
        }

        public int FrameCount
        {
            get 
            {
                if (_owningSheet == null)
                {
                    throw new AAException("OwningSheet must be set before calling this!");
                }

                return (int)Math.Floor((double)_owningSheet.ImageData.Width / (double)Width) * (int)Math.Floor((double)_owningSheet.ImageData.Height / (double)Height); 
            }
        }

        public Rectangle FrameArea(int index)
        {
            if (_owningSheet == null)
            {
                throw new AAException("OwningSheet must be set before calling this!");
            }

            int x = index % (int)Math.Floor((double)_owningSheet.ImageData.Width / (double)Width);
            int y = index % (int)Math.Floor((double)_owningSheet.ImageData.Height / (double)Height);

            return new Rectangle(x * (Width + HorizontalSpacing), y * (Height + VerticalSpacing), Width, Height);
        }

        public ISpriteSheetDivider Clone()
        {
            FixedSizeDivider c = new FixedSizeDivider();
            c.Width = Width;
            c.Height = Height;
            return c;
        }
    }
}
